/******************************************************************************
 * Copyright (C) 2008 Adam Parker. All rights reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining
 * a copy of this software and associated documentation files (the
 * "Software"), to deal in the Software without restriction, including
 * without limitation the rights to use, copy, modify, merge, publish,
 * distribute, sublicense, and/or sell copies of the Software, and to
 * permit persons to whom the Software is furnished to do so, subject to
 * the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 ******************************************************************************/
 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import javax.swing.event.*;
import javax.swing.plaf.basic.*;

public class EditableHeaderUI extends BasicTableHeaderUI 
{
    
	protected MouseInputListener createMouseInputListener() 
	{
		return new MouseInputHandler((EditableHeader)header);
	}

	public class MouseInputHandler extends BasicTableHeaderUI.MouseInputHandler 
	{
		private Component dispatchComponent;
		protected EditableHeader header;

		public MouseInputHandler(EditableHeader header) 
		{
			this.header = header;
		}

		private void setDispatchComponent(MouseEvent e) 
		{ 
			Component editorComponent = header.getEditorComponent();
			Point p = e.getPoint();
			Point p2 = SwingUtilities.convertPoint(header, p, editorComponent);
			dispatchComponent = SwingUtilities.getDeepestComponentAt(editorComponent, p2.x, p2.y);
		}

		private boolean repostEvent(MouseEvent e) 
		{ 
			if (dispatchComponent == null) 
			{
				return false; 
			}
			MouseEvent e2 = SwingUtilities.convertMouseEvent(header, e, dispatchComponent);
			dispatchComponent.dispatchEvent(e2); 
			return true; 
		}
    
		public void mousePressed(MouseEvent e) 
		{
			if (!SwingUtilities.isLeftMouseButton(e)) 
			{
				return;
			}
			super.mousePressed(e);

			if (header.getResizingColumn() == null) 
			{
		        Point p = e.getPoint();
		        TableColumnModel columnModel = header.getColumnModel();
		        int index = columnModel.getColumnIndexAtX(p.x);
		        if (index != -1) 
				{
					if (header.editCellAt(index, e))
					{
			            setDispatchComponent(e); 
			            repostEvent(e); 
					}
				}
			}
		}

		public void mouseReleased(MouseEvent e)
		{
			super.mouseReleased(e);
			if (!SwingUtilities.isLeftMouseButton(e))
			{
				return;
			}
			repostEvent(e); 
			dispatchComponent = null;    
		}
        
	} 
}